import store from "@/store"
import { getToken } from "./auth"
import ServerURL from '@/config'
export default {
    install(Vue) {
        //存放共享数据
        Vue.prototype.$simpleStore = function (name, object) {
            // store.commit('echart/setMap',{[name]:object})
            store.commit('echart/setMap', object)
        }
        //读取共享数据
        Vue.prototype.$simpleData = function (name) {
            const map = store.state.echart.echartOption
            // let obj = null
            // if(map.has(name)){
            //     obj = map.get(name)
            // }
            return map
        }
        //监听屏幕大小调整dialog宽度(使用时，记得调用$unResizeDialog清除)
        Vue.prototype.$resizeDialog = function (size, width) {
            window.onresize = () => {
                return (() => {
                    var val = document.body.clientWidth
                    const def = size // 默认宽度
                    if (val < def) {
                        width = '100%'
                    } else {
                        width = def + 'px'
                    }
                })()
            }
        }
        //清除window的监听效果
        Vue.prototype.$unResizeDialog = function () {
            window.onresize = null;
        }
        //建立websocket连接
        Vue.prototype.$webSocket = function (route) {
            // let path = '192.168.31.85:8001'
            let path = ServerURL.replaceAll('http://', '')
            // console.log(path, 'path');
            return new Promise((res, req) => {
                if ('WebSocket' in window) {
                    const ws = new WebSocket(`${'ws://' + path + `/genecloud/ws/message?type=${route}`}`, [getToken()])
                    res(ws)
                } else {
                    req('您的浏览器不支持 WebSocket!')
                }
            })
        }

        //下载svg/png/jpg/pdf
        Vue.prototype.$dropDown = function (res, title) {
            let url = window.URL.createObjectURL(new Blob([res]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", title);
            document.body.appendChild(link);
            link.click();
        }

        //获取公用数据
        Vue.prototype.$publicData = function (value) {
            return this.$store.state.echart[value]
        }
        //文件大小处理
        Vue.prototype.$size2Str = function (size) {
            if (size < 1024) {
                return size + 'B';
            } else if (size >= 1024 && size < Math.pow(1024, 2)) {
                return parseFloat(size / 1024).toFixed(2) + 'K';
            } else if (size >= Math.pow(1024, 2) && size < Math.pow(1024, 3)) {
                return parseFloat(size / Math.pow(1024, 2)).toFixed(2) + 'M';
            } else if (size >= Math.pow(1024, 3)) {
                return parseFloat(size / Math.pow(1024, 3)).toFixed(2) + 'G';
            } else {
                return 0 + 'B';
            }
        }

        //文件大小处理
        Vue.prototype.$allSizeDeal = function (allSize, size) {
            if (allSize < 1024) {
                return size;
            } else if (allSize >= 1024 && allSize < Math.pow(1024, 2)) {
                return parseFloat(size / 1024).toFixed(2);
            } else if (allSize >= Math.pow(1024, 2) && allSize < Math.pow(1024, 3)) {
                return parseFloat(size / Math.pow(1024, 2)).toFixed(2);
            } else if (allSize > Math.pow(1024, 3)) {
                return parseFloat(size / Math.pow(1024, 3)).toFixed(2);
            } else {
                return 0;
            }
        }

        //sideBar菜单设置
        Vue.prototype.$skipRouter = function (type, router) {
            if (!getToken()) return this.$message('请先登录！')
            this.$store.commit('menu/SIDEBAR_MENU', type)
            this.$router.push(`/${router}`)
        }
        //url地址转换对象
        Vue.prototype.$queryToObjectsearch = function (search) {
            const res = {}
            const paramArr = new URLSearchParams(search);
            paramArr.forEach((val, key) => {
                res[key] = val;
            })
            return res;
        }
        //url地址转换对象
        Vue.prototype.$url = function (url) {
            if (url.includes('cloudtutus.com')) {
                return 'http://cloudtutu.com.cn/'
            } else {
                return url
            }
        }

        //获取任务号
        Vue.prototype.$taskTitle = function (url) {
            let obj = new Date;
            //获取年月日，时分秒
            let a = obj.getFullYear();
            let b = obj.getMonth() + 1;   //月份从0月开始，所以需要+1
            let c = obj.getDate();
            let d = obj.getHours();
            let e = obj.getMinutes();
            let f = obj.getSeconds();
            let num = Math.ceil(Math.random() * 1000);
            if (('' + b).length < 2) b = '0' + b
            if (('' + c).length < 2) c = '0' + c
            if (('' + d).length < 2) d = '0' + d
            if (('' + e).length < 2) e = '0' + e
            if (('' + f).length < 2) f = '0' + f
            return `${a}${b}${c}${d}${e}${f} - ${num}`
        }
    }
}